# 消息中心模块

from flask import Flask, render_template, request, redirect,Blueprint
from flask_login import login_required,current_user
from app.models import Notification
from app import db

bp=Blueprint('notification',__name__,url_prefix='/notifications')

# http://localhost:5000/notifications/
@bp.route('/')
@login_required
def index():
    notifications=(Notification.query
                   .filter_by(recipient_id=current_user.id)
                   .order_by(Notification.create_time.desc())
                   .all())
    return render_template('notifications.html',notifications=notifications)


# 标记消息是否已读
@bp.route('/mark_read/<int:notification_id>')
@login_required
def mark_read(notification_id):
    notification=Notification.query.get_or_404(notification_id)
    if notification.recipient_id==current_user.id:
        notification.is_read=True
        db.session.commit()
    return render_template('notifications.html',notifications=current_user.notifications_received)

